package com.laowang.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.laowang.common.utils.DateUtils;
import com.laowang.framework.redis.RedisCache;
import com.laowang.project.system.domain.*;
import com.laowang.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyLifeCircleCommentMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生活圈评论Service业务层处理
 *
 * @author laowang
 * @date 2023-04-25
 */
@Service
public class MyLifeCircleCommentServiceImpl implements MyLifeCircleCommentService {
    @Autowired
    private MyLifeCircleCommentMapper myLifeCircleCommentMapper;

    @Autowired
    private MyLifeCircleService myLifeCircleService;

    @Autowired
    private MyCyhSocreService myCyhSocreService;

    @Autowired
    private WxUserService  wxUserService;

    @Autowired
    private UserPointLogService userPointLogService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询生活圈评论
     *
     * @param id 生活圈评论ID
     * @return 生活圈评论
     */
    @Override
    public MyLifeCircleComment selectMyLifeCircleCommentById(Integer id) {
        return myLifeCircleCommentMapper.selectMyLifeCircleCommentById(id);
    }

    /**
     * 查询生活圈评论列表
     *
     * @param myLifeCircleComment 生活圈评论
     * @return 生活圈评论
     */
    @Override
    public List<MyLifeCircleComment> selectMyLifeCircleCommentList(MyLifeCircleComment myLifeCircleComment) {
        return myLifeCircleCommentMapper.selectMyLifeCircleCommentList(myLifeCircleComment);
    }

    /**
     * 新增生活圈评论
     *
     * @param myLifeCircleComment 生活圈评论
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMyLifeCircleComment(MyLifeCircleComment myLifeCircleComment) {
        myLifeCircleComment.setCreateTime(DateUtils.getNowDate());
        synchronized (this){
            MyLifeCircle myLifeCircle = myLifeCircleService.selectMyLifeCircleById(myLifeCircleComment.getCircleLifeId());
            Integer commentNum = myLifeCircle.getCommentNum();
            commentNum++;
            myLifeCircle.setCommentNum(commentNum);
            myLifeCircleService.updateMyLifeCircle(myLifeCircle);
        }

        return myLifeCircleCommentMapper.insertMyLifeCircleComment(myLifeCircleComment);
    }

    /**
     * 修改生活圈评论
     *
     * @param myLifeCircleComment 生活圈评论
     * @return 结果
     */
    @Override
    public int updateMyLifeCircleComment(MyLifeCircleComment myLifeCircleComment) {
        return myLifeCircleCommentMapper.updateMyLifeCircleComment(myLifeCircleComment);
    }

    /**
     * 批量删除生活圈评论
     *
     * @param ids 需要删除的生活圈评论ID
     * @return 结果
     */
    @Override
    public int deleteMyLifeCircleCommentByIds(Integer[] ids) {
        return myLifeCircleCommentMapper.deleteMyLifeCircleCommentByIds(ids);
    }

    /**
     * 删除生活圈评论信息
     *
     * @param id 生活圈评论ID
     * @return 结果
     */
    @Override
    public int deleteMyLifeCircleCommentById(Integer id) {
        return myLifeCircleCommentMapper.deleteMyLifeCircleCommentById(id);
    }

    @Transactional
    @Override
    public Integer addLifeComment(MyLifeCircleComment myLifeCircleComment) {
        insertMyLifeCircleComment(myLifeCircleComment);
        //用户加积分  评论人加积分
        Integer userId = myLifeCircleComment.getUserId();
        Integer i = null;
        MyCyhSocre myCyhSocre = myCyhSocreService.selectMyCyhSocreByType(1);
        String message = myLifeCircleComment.getMessage();
        if (myCyhSocre !=null && message.length()>=15){
            synchronized (this){
                Integer score = myCyhSocre.getScore();
                Integer upperLimit = myCyhSocre.getUpperLimit();
                //是否已经满次数了
                //没有副牌每天看一次
                CacheObj cacheObj = redisCache.getCacheObject("comment:" + userId);
                if (Objects.isNull(cacheObj)){
                    CacheObj cacheObj1 = new CacheObj();
                    cacheObj1.setDate(new Date());
                    cacheObj1.setNum(1);
                    redisCache.setCacheObject("comment:"+userId,cacheObj1,2, TimeUnit.DAYS);

                }else {
                    Integer num = cacheObj.getNum();
                    Date date = cacheObj.getDate();
                    Date date1 = new Date();
                    if (DateUtils.isSameDay(date1,date)){
                        if (num.equals(upperLimit)){
                            return 0;
                        }
                        num++;
                        cacheObj.setNum(num);
                        redisCache.setCacheObject("comment:"+userId,cacheObj,2, TimeUnit.DAYS);
                    }else {

                        CacheObj cacheObj1 = new CacheObj();
                        cacheObj1.setDate(new Date());
                        cacheObj1.setNum(1);
                        redisCache.setCacheObject("comment:"+userId,cacheObj1,2, TimeUnit.DAYS);

                    }

                }

                WxUser wxUser = wxUserService.selectWxUserById(userId);
                Integer points = wxUser.getPoints();
                Integer newPoint = points + score;
                wxUser.setPoints(newPoint);
                wxUserService.updateWxUser(wxUser);
                //积分日志
                UserPointLog userPointLog = new UserPointLog();
                userPointLog.setRemark("评论加积分");
                userPointLog.setOldPoint(points);
                userPointLog.setChangePoint(score);
                userPointLog.setNewPoint(newPoint);
                userPointLog.setUserId(userId);
                userPointLog.setType(1);//1评论,2点赞,3发车友圈
                userPointLog.setCreated(new Date());
                userPointLogService.insertUserPointLog(userPointLog);
                i = score;
            }
        }

        return i;
    }
}
